package com.t194.service.impl;

import com.t194.mapper.PermissionMapper;
import com.t194.mapper.RoleMapper;
import com.t194.mapper.RolepermissionMapper;
import com.t194.pojo.Permission;
import com.t194.pojo.Role;
import com.t194.service.RoleService;
import com.t194.vo.NodeState;
import com.t194.vo.TreeNode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

@Service("roleService")
@Transactional
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private PermissionMapper permissionMapper;
    @Resource
    private RolepermissionMapper rolepermissionMapper;
    @Override
    public List<Role> searchAll() {
        return roleMapper.findAll();
    }

    @Override
    public void batchDel(Integer[] ids) {
        roleMapper.batchDel(ids);
    }

    @Override
    public int add(Role role) {
        return roleMapper.add(role);
    }

    @Override
    public void update(Role role) {
        roleMapper.update(role);
    }

    @Override
    public List<TreeNode> search4Tree(Integer roleId) {
        List<Permission> list=permissionMapper.findByParentId(null);
        return list.stream()
                .map(perm->{
                    TreeNode node01=transfer(perm,roleId);
                    Integer id=node01.getId();
                    List<Permission> list2=permissionMapper.findByParentId(id);
                    for(Permission p2:list2){
                        TreeNode node02 =transfer(p2,roleId);
                        node01.getNodes().add(node02);
                        List<Permission> list3=permissionMapper.findByParentId(p2.getId());
                        list3.forEach(p3->{
                            TreeNode node03=transfer(p3,roleId);
                            node02.getNodes().add(node03);
                        });
                    }
                    return node01;
                })

        .collect(toList());
    }

    @Override
    public void grant(Integer roleId, Integer[] ids) {
        rolepermissionMapper.batchDel(roleId);
        if(ids!=null&&ids.length>0){
            for (Integer id:ids){
                rolepermissionMapper.add(roleId,id);
            }
        }
    }

    private TreeNode transfer(Permission permission,Integer roleId){
        TreeNode treeNode=new TreeNode();
        treeNode.setId(permission.getId());
        treeNode.setText(permission.getName());
        int count=permissionMapper.count(treeNode.getId());
        if(count>0){
            treeNode.setNodes(new ArrayList<>());
        }
        NodeState state=new NodeState();
        treeNode.setState(state);
        List<Integer> permIds= rolepermissionMapper.findPermIds(roleId);
        if (permIds!=null&&permIds.contains(permission.getId())){
            state.setChecked(true);
        }
        return treeNode;
    }

}
